﻿using SecsEquip;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EquipSecs300
{
    public partial class LogsView : CMyFormEx
    {
        ConcurrentQueue<string> logsQueue = new ConcurrentQueue<string>();
        public LogsView()
        {
            InitializeComponent();
        }
        public override void UpdateData()
        {
            secsEquipHelper.AcLogMsg += AddLog;
            secsEquipHelper.Alarm.AcLogMsg += AddLog;
            secsEquipHelper.Even.AcLogMsg += AddLog;
            secsEquipHelper.Variables.AcLogMsg += AddLog;
            secsEquipHelper.RemoteContro.AcLogMsg += AddLog;
            secsEquipHelper.Recipe.AcLogMsg += AddLog;
            secsEquipHelper.PPFormat.AcLogMsg += AddLog;
            secsEquipHelper.Constant.AcLogMsg += AddLog;
            secsEquipHelper.PPCostomize.AcLogMsg += AddLog;
            secsEquipHelper.LoadPort.AcLogMsg += AddLog;
            secsEquipHelper.PJob.AcLogMsg += AddLog;
            secsEquipHelper.CJob.AcLogMsg += AddLog;
            secsEquipHelper.LoadPort.AcLogMsg += AddLog;
            for (int i = 1; i < secsEquipHelper.LoadPort.m_nPortCount + 1; i++)
            {
                secsEquipHelper.LoadPort.MapPort[$"Port_{i}"].AcLogMsg += AddLog;
            }
            secsEquipHelper.SubstrateTracking.AcLogMsg += AddLog;
        
        }

        DateTime dateTimePre = DateTime.Now;
        public List<string> logListPre = new List<string>();
        string logPre = "";

        private void timer1_Tick(object sender, EventArgs e)
        {
            PrintLogs();
        }

        public void AddLog(string log)
        {
            if (string.IsNullOrEmpty(log))
            {
                return;
            }
            var durTime = DateTime.Now - dateTimePre;
            bool isExist = false;
            for (int i = 0; i < logListPre.Count; i++)
            {
                if (logListPre[i].Equals(log))
                {
                    isExist = true;
                }
            }

            if (logListPre.Count > 5)
            {
                logListPre.RemoveAt(0);
            }
            if (durTime.TotalMilliseconds > 200 || !isExist)
            {
                if (logsQueue.Count > 1000000)
                {
                    logsQueue = new ConcurrentQueue<string>();
                    GC.Collect();
                }
                logsQueue.Enqueue($"{DateTime.Now.ToString("MM/dd HH:mm:ss.fff")}  {log}");
                dateTimePre = DateTime.Now;
                logPre = log;
                logListPre.Add(logPre);
            }

        }

        public void PrintLogs()
        {
            if (logsQueue.Count > 0)
            {
                string[] msgArr = new string[1];

                bool b = logsQueue.TryDequeue(out string strlog);
                if (b)
                {
                    msgArr = strlog.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
                }

                lbxLog.BeginUpdate();
                for (int i = 0; i < msgArr.Length; i++)
                {
                    lbxLog.Items.Add(msgArr[i]);
                }
                lbxLog.Items.Add(Environment.NewLine);

                while (lbxLog.Items.Count > 1000)
                {
                    lbxLog.Items.RemoveAt(0);
                }

                this.lbxLog.SelectedIndex = this.lbxLog.Items.Count - 1;
                lbxLog.EndUpdate();
            }
        }

        private string AppenLog()
        {
            StringBuilder sb = new StringBuilder();
            while (logsQueue.Count > 0)
            {
                string[] msgArr = new string[1];

                bool b = logsQueue.TryDequeue(out string strlog);
                if (b)
                {
                    msgArr = strlog.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
                }
                for (int i = 0; i < msgArr.Length; i++)
                {
                    sb.AppendLine(msgArr[i]);
                    //sb.AppendLine(Environment.NewLine);
                }

            }
            return sb.ToString();
        }



        private void Log_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }
    }
}
